import { _decorator, Animation, BoxCollider2D, Collider2D, Component, Contact2DType, IPhysics2DContact, Node, RigidBody2D, v2 } from 'cc';
import { tagEnum } from './Enum';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('Player')
export class Player extends Component {

    static ins:Player=null;
    rig2D:RigidBody2D=null;
    animat:Animation=null;
    collider=null;

    protected onLoad(): void {
        Player.ins=this;

        this.rig2D=this.node.getComponent(RigidBody2D);
        this.collider=this.node.getComponent(BoxCollider2D);
        this.animat=this.node.getComponent(Animation);
    }

    start() {
        this.collider.on(Contact2DType.BEGIN_CONTACT,this.onBeginContact,this)
    }

    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null){

        console.log(otherCollider,"otherCollider接触到了",otherCollider.tag)
        if (otherCollider.tag==tagEnum.zhangaiwu) {
            console.log("游戏失败")
            GameMgr.ins.gameOver();
        }

        if (otherCollider.tag == tagEnum.dimian) {//掉到地上
            if (GameMgr.ins.jumpNum!=0) {
                GameMgr.ins.jumpNum = 0;
                this.animat.play("playerRun")
            }
        }

        if (otherCollider.tag==tagEnum.defen) {
            console.log("得分")
            GameMgr.ins.PlayerScore+=1;
        }
    }

    jump(){
        console.log("跳")
        this.rig2D.applyForceToCenter(v2(0,1100),true);
        console.log( this.animat," this.animat")
        this.node.getComponent(Animation).pause()
        // this.rig2D.linearVelocity=v2(0,15);
    }

   
}


